using System;
using System.Linq;
using JNPF.Utils;
using System.Threading.Tasks;
using JNPF.Data;
using JNPF.Data.Util;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using JNPF.Basics.Models;
using NPOI.POIFS.FileSystem;

namespace JNPF.Basics.Logic
{
    public class CustomerattachmentService : DataFactory, CustomerattachmentIService
    {
        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        public async Task<CustomerattachmentEntity> GetInfo(int id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<CustomerattachmentEntity>(m => m.Id == id);
            }
        }

        /// <summary>
        /// 列表（分页）
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<CustomerattachmentEntity>> GetPageList(CustomerattachmentListQueryInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<CustomerattachmentEntity>();
                var queryParam = input.queryJson.ToObject();
                //查询附件名称
                if (!string.IsNullOrEmpty(input.attachmentName))
                {
                    queryWhere = queryWhere.And(m => m.AttachmentName.Contains(input.attachmentName));
                }

                if (input.customerId.HasValue)
                {
                    queryWhere = queryWhere.And(m => m.CustomerId == input.customerId.Value);
                }
                if (!string.IsNullOrEmpty(input.businessId))
                {
                    queryWhere = queryWhere.And(m => m.BusinessId == input.businessId);
                }
                if (!string.IsNullOrEmpty(input.attachmentType))
                {
                    queryWhere = queryWhere.And(m => m.AttachmentType == input.attachmentType);
                }

                queryWhere = queryWhere.And(m => m.DeleteMark == 0);

                return await db.GetIQueryable<CustomerattachmentEntity>().Where(queryWhere).GetPageResultAsync(input);
            }
        }

        /// <summary>
        /// 创建信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(CustomerattachmentEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }



        /// <summary>
        /// 创建信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(List<CustomerattachmentEntity> multList) 
        {

            using (var db = this.Basic)
            {
                return await db.InsertAsync(multList);
            }
        }



        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(CustomerattachmentEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.UpdateAsync(entity);
            }
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(CustomerattachmentEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync(entity);
            }
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<List<CustomerattachmentEntity>> GetList(CustomerattachmentListQueryInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<CustomerattachmentEntity>();
                //查询附件名称
                if (!string.IsNullOrEmpty(input.attachmentName))
                {
                    queryWhere = queryWhere.And(m => m.AttachmentName.Contains(input.attachmentName));
                }
                queryWhere = queryWhere.And(m => m.DeleteMark == 0);
                return await db.GetIQueryable<CustomerattachmentEntity>().Where(queryWhere).ToListAsync();
            }
        }


    }
}